/*
* $Id: AddInspectOperation.cpp 336 2011-09-28 06:06:36Z volok-aleksej@yandex.ru $
*
* Copyright 2009-2011 by Volokitin Aleksey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddInspectOperation.h"
#include "Database/TransactManager.h"
#include "Database/Tables/JournalTable.h"
#include "Database/Tables/InspectTable.h"
#include "Database/Tables/FieldValuesTable.h"

CAddInspectOperation::CAddInspectOperation(CTransactManager* pManager)
: m_pManager(pManager){}
CAddInspectOperation::~CAddInspectOperation(){}

std::tstring CAddInspectOperation::ExecuteOperation(IDatabase* pDatabase, IReader* pReader){
	CJournalTable journal;
	CInspectTable inspect;
	CFieldValuesTable fieldValue;
	int size;
	pReader->ReadInt(size);
	int lenData = 0;
	int idJournal;
	lenData = pReader->ReadInt(idJournal);
	int idFamily;
	lenData += pReader->ReadInt(idFamily);
	journal.m_content.s_nIdFamily = m_pManager->GetRealId(
		CTransactManager::ID(CTransactManager::FAMILY, idFamily));
	lenData += pReader->ReadTime(journal.m_content.s_date);
	lenData += pReader->ReadInt(journal.m_content.s_nFrame);
	lenData += pReader->ReadInt(journal.m_content.s_nStrength);
	lenData += pReader->ReadInt((int&)journal.m_content.s_QueenBee);
	lenData += pReader->ReadInt(journal.m_content.s_nWax);
	lenData += pReader->ReadInt(journal.m_content.s_nAmbrosia);
	lenData += pReader->ReadInt(journal.m_content.s_nEvacuation);
	lenData += pReader->ReadInt(journal.m_content.s_nFeeding);
	lenData += pReader->ReadInt((int&)journal.m_content.s_disease);
	lenData += pReader->ReadUTF(journal.m_content.s_strNote);
	int idRecord;
	lenData += pReader->ReadInt(idRecord);
	if(idRecord){
		journal.m_content.s_nIdRecord = fieldValue.GetMaxId(pDatabase) + 1;
		m_pManager->AddIDMap(CTransactManager::ID(CTransactManager::FIELD_VALUE, idRecord),
			journal.m_content.s_nIdRecord);
	}
	journal.SetValue(pDatabase);
	m_pManager->AddIDMap(CTransactManager::ID(CTransactManager::JOURNAL, idJournal),
						journal.m_content.s_nId);
	int sizeInspect;
	lenData += pReader->ReadInt(sizeInspect);
	for(int i = 0; i < sizeInspect; i++){
		lenData += pReader->ReadInt(idJournal);
		inspect.m_content.s_nIdJournal = m_pManager->GetRealId(
			CTransactManager::ID(CTransactManager::JOURNAL, idJournal));
		int idHousing;
		lenData += pReader->ReadInt(idHousing);
		if(idHousing)
			idHousing = m_pManager->GetRealId(CTransactManager::ID(CTransactManager::JOURNAL, idHousing));
		inspect.m_content.s_nIdHousing = idHousing;
		lenData += pReader->ReadUTF(inspect.m_content.s_strNameHousing);
		lenData += pReader->ReadInt(inspect.m_content.s_nHoney);
		lenData += pReader->ReadInt(inspect.m_content.s_nBrood);
		lenData += pReader->ReadInt(inspect.m_content.s_nOpenBrood);
		lenData += pReader->ReadInt(inspect.m_content.s_nCloseBrood);
		lenData += pReader->ReadInt(inspect.m_content.s_nGoodsHoney);
		inspect.SetValue(pDatabase);
	}
	while (lenData < size){
		lenData += pReader->ReadInt(idRecord);
		fieldValue.m_content.s_nId = m_pManager->GetRealId(
			CTransactManager::ID(CTransactManager::FIELD_VALUE, idRecord));
		lenData += pReader->ReadUTF(fieldValue.m_content.s_strName);
		lenData += pReader->ReadUTF(fieldValue.m_content.s_strValue);
		lenData += pReader->ReadInt((int&)fieldValue.m_content.s_type);
		fieldValue.SetValue(pDatabase);
	}
	std::tstring strLog = ILanguageManager::GetLanguageManager()->GetString(ADD_INSPECT_LOG_ID);
	return strLog;
}

void CAddInspectOperation::Free(){
	delete this;
}
